<?php
/**
 * @page geomap Geomap
 * 
 * Geomap feature allows you to inset Google map related to listings. It also allows your visitors to perform
 * distance based search. 
 * 
 * @section configure_geomap Configuring Geomap features
 * 
 * First of all you will need to enter the license for Geomap plugin at Backend > Settings > Licenses
 * 
 * After that goto Backend > Settings > Map
 * 
 * You should see some fields which are explained below in details:
 * 
 * <b>Country region</b>
 * This field is used to set preferred country. This is used in case of ambiguity while places search.
 * 
 * <b>Yahoo placefinder appId</b>
 * This is the API key which can be obtained from link given below this field. To resolve addresses, Crowd Vox
 * will call Yahoo servers.
 * 
 * <b>Address Fields</b>
 * Address1 Field, Address2 Field, City Field, State Field, Postal Code Field, Country Field, Search Field
 * These are the fields which are sent to resolution service. Now you need to map your custom fields with
 * these fields so correct data is sent over to resolution service.
 * 
 * When you have geomapping enabled, you can also use @subpage map_module to put map on different pages. Map for listing view page comes automatically and can be edited from file- app/views/listings/view.ctp file
 * 
 * 
 * Also see: @subpage map_pointers
 * 
 */
	App::import("Model","Config.BaseConfig");
	class MapConfig extends BaseConfig
	{
		public $name = "MapConfig";
		
		public $fieldProperties = array(
		);
		var $cache = true;
		function initialize()
		{
			$this->fieldProperties = array(
			'country_region'=> array("type"=>"text",'label'=>"Country region",'after'=>__("Same as country code,with general exception for uk as gb.")),
			"address1_field"=>array("type"=>"text","group"=>"address_field"),
			"address2_field"=>array("type"=>"text","group"=>"address_field"),
			"city_field" => array("type"=>"text","group"=>"address_field"),
			"state_field" => array("type"=>"text","group"=>"address_field"),
			"postal_code_field"=>array("type"=>"text","group"=>"address_field"),
			"country_field"=>array("type"=>"text","group"=>"address_field"),
			"search_field"=>array("type"=>"text","group"=>"address_field"),
			"radius_metric"=>array("type"=>"select","options"=>array("mi"=>"Miles","km"=>"K.M")),
			"show_distance"=>array("type"=>"checkbox",'default'=>1,'label'=>"Show distance on search."),
            "show_form_map" => array('default'=>1,'type'=>"checkbox","label"=>__("Show map while adding listing and allow drag drop marker to change location."))
         );
			$mapFields = $this->getMapingFields();
			foreach($this->fieldProperties as $field => $property)
			{
				if(in_array($field,$mapFields))
				{
					$this->fieldProperties[$field] = am($this->fieldProperties[$field],array("type"=>"select",'options'));
				}
				$this->fieldProperties[$field]['group'] = "add";
			}
			parent::initialize();
		}
		
		function setFieldOptions()
		{
			$customFields = cri("CustomField")->find("all",array("conditions"=>array(),"fields"=>array("id","name","type",'label'),"recursive"=>-1,'order'=>"label"));
			$fieldsOptions = array();
			foreach($customFields as $customField)
			{
				$fieldsOptions[$customField['CustomField']['name']] = $customField['CustomField']['label']."(".$customField['CustomField']['type'].")";
			}
			foreach($this->getMapingFields() as $field)
			{
				if(preg_match("/_field$/",$field))
				{
					$this->fieldProperties[$field]['options'] = $fieldsOptions;
					$this->fieldProperties[$field]['empty'] = __("-Select-");
				}
			}
		}
		
		function getMapingFields()
		{
			$fields = array();
			foreach($this->fieldProperties as $field => $property)
			{
				if(preg_match("/_field$/",$field))
				{
					$fields[] = $field;
				}
			}
			return $fields;
		}
		
		function getDistanceBetweenPoints($point1,$point2)
		{
			$distance = GeoUtility::getDistanceBetweenPoints($point1,$point2);
			if(cr('MapConfig.radius_metric') == "km")
			{
				$distance = Utility::milesToKm($distance);
			}
			return round($distance,2);
		}
        
        function getLocationFields()
        {
          $locationFields = array();
          foreach(cr("MapConfig") as $key => $value)
          {
            if(preg_match("/_field$/",$key) && strlen($value))
            {
              $locationFields[] = $value;
            }
          }
          return array_unique($locationFields);
        }
		
		function init()
		{
			parent::init();
			if(!cr("MapConfig.country_region") && cr("MapConifg.country_domain"))
			{
				Configure::write("MapConfig.country_region",cr("MapConifg.country_domain"));
			}
			Configure::write("Geo.region",cr("MapConfig.country_region"));
			Configure::write("Geo.domain",cr("MapConfig.country_domain"));
			Configure::write("Geo.yahooAppId",cr("MapConfig.yahooAppId"));
		}
        
	}
?>